package logic

import (
	"gameserver/messages"
	"gameserver/server"
	"gameserver/server/Gateway/internal"

	"gitee.com/night-tc/gobige/logger"
	"gitee.com/night-tc/gobige/msgdef/protomsg"
	"gitee.com/night-tc/gobige/sess"
)

// 用户消息
type Player_Proc struct {
	Player *Player
}

// 处理连接被关的时候的消息
func (this *Player_Proc) MsgProc_SessClosed(msg *protomsg.SessClosed) {
	logger.Infof("SessClosed uid:%d;", msg.GetSessUID())
	this.Player.SetClientSess(nil)
	//用户的连接被关掉的时候,需要做掉线处理
	//这个时间可以根据需要从配置表获取
	this.Player.CallDelay(func() {
		_ = this.Player.Post(server.ServerType_Game, &messages.PlayerOfflineSrv{})
		this.Player.outtimeid = this.Player.Timer.OnceTimer(internal.GetSrvinst().GetSInfo().UserCacheTime, func(i ...interface{}) {
			this.Player.LogoutPlayer()
		})
	})

}

// 处理客户端发过来的消息
func (this *Player_Proc) MsgProc_EntityRPCReq(req *protomsg.EntityRPCReq) {
	if req.SrvType == internal.GetSrvinst().GetSInfo().GetDefType() {
		//这个消息是本服务器的消息,需要把消息解开
		if fmsg, err := sess.DecodeMsg(0, req.MsgContent); err != nil {
			logger.Error("MsgProc_EntityRPCReq:", err)
		} else {
			this.Player.FireRPC(req.PRCName, fmsg)
		}
	} else {
		//这个消息是其他服务器的消息,需要转发给其他服务器
		//这里可以根据需要做一些处理
		srvinfo, err := this.Player.GetSrvByStype(req.SrvType)
		if err != nil {
			logger.Error("GetSrvByStype error:", err)
			return
		}
		msg := new(protomsg.ForwardRPCSrv)
		msg.EntityID = this.Player.GetEntityID()
		msg.SpaceID = srvinfo.SpaceID
		msg.SrvID = srvinfo.SrvID
		msg.SrvType = req.SrvType
		msg.RPCName = req.PRCName
		msg.MsgContent = req.MsgContent
		internal.GetSrvinst().PostSess(srvinfo.SrvID, msg)
	}
}
